package org.jeecg.modules.labourwork.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.labourwork.entity.WorkOrder;
import org.jeecg.modules.labourwork.service.WorkOrderService;
import org.jeecg.modules.labourwork.vo.WorkingOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(tags = "单表DEMO")
@RestController
@RequestMapping("/workorder")
public class WorkOrderController {

    @Autowired
    private WorkOrderService orderService;

    @GetMapping(value = "/findwork")
    public Result<IPage<WorkOrder>> findWorkOrder(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize",defaultValue = "5")Integer pageSize,
                                                  WorkOrder workOrder){
        Result<IPage<WorkOrder>> result = new Result<>();
        Page page = new Page(pageNo,pageSize);
        IPage<WorkOrder> workOrderIPage = orderService.findWorkOrderAll(page,workOrder);
        workOrderIPage.setTotal((long)orderService.findWorkOrderCount(workOrder));
        result.setResult(workOrderIPage);
        return result;
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation("工程工单")
    public Result<IPage<WorkingOrderVo>> list(WorkingOrderVo workingOrderVo,
                                              @RequestParam(name = "pageNo",defaultValue="1") Integer pageNo,
                                              @RequestParam(name = "pageSize",defaultValue="1") Integer pageSize){
        Result<IPage<WorkingOrderVo>> result = new Result<IPage<WorkingOrderVo>>();
        //分页类
        Page<WorkingOrderVo> page=new Page<WorkingOrderVo>(pageNo,pageSize);
        //获得查询结果i
        IPage<WorkingOrderVo> iPage = orderService.findWrok(page,workingOrderVo);
        iPage.setTotal((long)orderService.countWork(workingOrderVo));
        result.setResult(iPage);
        return result;
    }

    @RequestMapping(value = "/addwork",method = RequestMethod.POST)
    public Result<WorkOrder> addWorkOrder(WorkOrder workOrder){
        Result<WorkOrder> result = new Result<>();
        if (orderService.save(workOrder)){
             result.setMessage("添加成功！");
        } else {
          result.setMessage("添加失败！");
        }
        return result;
    }

    @RequestMapping(value = "/updwork",method = RequestMethod.POST)
    public Result<WorkOrder> updWorkOrder(WorkOrder workOrder){
         Result<WorkOrder> result = new Result<>();
         if (orderService.updateById(workOrder)){
             result.setMessage("修改成功！");
         }else {
             result.setMessage("修改失败！");
         }
        return result;
    }

    @RequestMapping(value = "/delwork",method = RequestMethod.GET)
    public Result<WorkOrder> delWorkOrder(WorkOrder workOrder){
        Result<WorkOrder> result = new Result<>();
        if (orderService.removeById(workOrder.getWorkId())){
            result.setMessage("删除成功");
        }else {
            result.setMessage("删除失败！");
        }
        return result;
    }
}
